<?php
if(@$_POST['open'] != 'please'){
	exit;
}
include_once("../../php/include_all.php");
?>
<div class="row-fluid">
	<div class="span12">
		<h3 class="page-title">
            <b>Master Data Bidang</b>
        </h3>
		<div class="widget">
			<div class="widget-title" style="margin-top: 20px;">
                <h4><i class="icon-reorder"></i>Data Bidang</h4>
                <span class="tools">
                    <a href="javascript:;" class="icon-chevron-down"></a>
                </span>
            </div>
			<div class="widget-body form">
				<form id="form-bidang" method="POST" action="" class="form-horizontal">
					<input type="hidden" name="act" value="save">
					<input type="hidden" id="id_bidang" name="id_bidang" value="0">
					<div class="control-group">
                        <label class="control-label">Bidang</label>
                        <div class="controls">
                            <input type="text" id="text-bidang" name="text-bidang" class="input-xlarge" />
                        </div>
					</div>
					<div class="form-actions">
                        <button type="button" class="btn btn-primary btn-simpan">Save</button>
                        <span id="result"></span>
                    </div>
				</form>
			</div>
		</div>
	</div>
	<div class="row-fluid">
        <div class="span12">
			<div class="widget" style="">
                <div class="widget-title">
                    <h4><i class="icon-reorder"></i>Tabel Bidang</h4>
                    <span class="tools">
                        <a href="javascript:;" class="icon-chevron-down"></a>
                    </span>
                </div>
				<div class="widget-body">
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th class="hidden-phone" style="width:20px; text-align:center;">No</th>
                            <th style="text-align:center;">Bidang</th>
							<th style="text-align:center;">Edit</th>
                        </tr>
                    </thead>
                    <tbody id="tbody-bidang">
						<?php
							$no = 1;
							$data = get_datas("SELECT * FROM spm_bidang_urusan");
							foreach ($data as $data){
						?>
						<tr>
							<td style="text-align:center;"><?php echo $no?></td>
							<td><?php echo $data['nama_bidang'] ?></td>
							<td style="width:200px; text-align:center;">	<a class="btn btn-info btn-edit" name="<?php echo $data['id_bidang']?>"><i class="icon-edit icon-white"></i>&nbsp; Edit</a>
									<a class="btn btn-danger btn-delete" name="<?php echo $data['id_bidang']?>"><i class="icon-trash icon-white"></i>&nbsp; Delete</a>
							</td>
						</tr>
						<?php
						$no++;
						}
						?>
					</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<script>
    jQuery(document).ready(function() {       
         // initiate layout and plugins
        App.init();
    });
	
</script>	
<script>	
	var urls = 'master_bidang/bidang_action.php';
	var hasil = $('#result');
	
	$('.btn-simpan').click(function(){
		var btn = $(this);
		var form = $('#form-bidang');
		var data = form.serializeArray();
		var post = $.post(urls, data);
		
		post.done(function(res){
			var result = res.split('__');
			if(result.length == 3){
				if(result[0] == 'success'){
					hasil.html('<font color="green">tersimpan... </font>');
					setTimeout(function(){
						hasil.html('');
					}, 1500);
					
					var tbody = $('#tbody-bidang');
					tbody.html(result[2]);
					go();
					$('#text-bidang').val('');
					$('#id_bidang').val(0);
				}else{
					hasil.html('<font color="red">'+res+'</font>');
				}
			}else{
				hasil.html('<font color="red">'+res+'</font>');
			}
		});
	});
	
	function go(){
		$('.btn-delete').click(function(){
			var btn = $(this);
			var pid = this.name;
			var load = $(this);
			bootbox.confirm("Anda akan menghapus bidang ini?", function(result) {
                if(result==true){
                    if(pid!=0){
                        var post = $.post(urls,{act:"delete",id_bidang:pid});
                        post.done(function(data){
                            var jadi = data.split('__');
								if(jadi.length == 3){
									if(jadi[0] == 'success'){
										hasil.html('<font color="green">Data Telah Dihapus...</font>');
										setTimeout(function(){
											hasil.html('');
										}, 1500);
										var tbody = $('#tbody-bidang');
										tbody.html(jadi[2]);
										go();
									//btn.parent().parent().remove();
									}
								}
                        });
                        load.html('<i class="icon-trash icon-white"></i>&nbsp; Delete');
                    }
                }else{
                    load.html('Delete');
                }
            });
		});
		
		$('.btn-edit').click(function(){
            var id_bidang= this.name;
            var form = $('#form-bidang');
            var post = $.post(urls,{act:'edit',id_bidang:id_bidang});
            post.done(function(res){
                var value = res.split('__');  
                //alert(res);
                $('#id_bidang').val(value[0]);
                form.find('input[name="text-bidang"]').val(value[1]);
		
            });
        });
	}
	
	go();
</script>